package com.atwisdom.star.core.dsl.functionClass.Impl;

import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.util.JsonUtil;
import com.atwisdom.star.common.vo.PageInfo;
import com.atwisdom.star.core.dao.pojo.functionClass.SimpleMap;
import com.atwisdom.star.core.dao.service.functionClass.SimpleMapService;
import com.atwisdom.star.core.dsl.functionClass.SimpleMapDslService;
import com.atwisdom.star.core.dsl.util.DslServiceUtil;
import com.atwisdom.star.exception.pojo.BusinessException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class SimpleMapDslServiceImpl implements SimpleMapDslService {
    private SimpleMapService simpleMapService;

    /**
     * 添加模型
     * @param jsonObject  接收参数
     * @return
     */
    @Override
    public JSONObject add(JSONObject jsonObject) {
        try {
            SimpleMap simpleMap = new SimpleMap(jsonObject);
            simpleMapService.save(simpleMap);
            return simpleMap.fetchJsonObject();
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }


    /**
     * 批量添加
     * @param jsonObjectList  接收参数
     * @return
     */
    @Override
    public List<JSONObject> batchAdd(List<JSONObject> jsonObjectList) {
        try {
            List<SimpleMap> simpleMaps = this.transformActionDataList(jsonObjectList);
            simpleMapService.saveBatch(simpleMaps);
            return JsonUtil.transformToJson(simpleMaps);
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }

    /**
     * 更新
     * @param jsonObject  接收参数
     * @return
     */
    @Override
    public JSONObject update(JSONObject jsonObject) {
        try {
            SimpleMap simpleMap = new SimpleMap(jsonObject);
            simpleMapService.updateById(simpleMap);
            return simpleMap.fetchJsonObject();
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }

    /**
     * 批量更新
     * @param jsonObjectList  接收参数
     * @return
     */
    @Override
    public List<JSONObject> batchUpdate(List<JSONObject> jsonObjectList) {
        try {
            List<SimpleMap> simpleMaps = this.transformActionDataList(jsonObjectList);
            boolean b = simpleMapService.updateBatchById(simpleMaps);
            return JsonUtil.transformToJson(simpleMaps);
        } catch (Exception e) {
            throw new BusinessException("999999", "操作失败！", e);
        }
    }


    /**
     * 根据id数组进行删除
     * @param ids 参数
     * @return 返回结果
     */
    @Override
    public boolean deleteByIds(List<Long> ids) {
        Integer delCount = simpleMapService.getBaseMapper().deleteBatchIds(ids);
        if(delCount >= 1){
            return true;
        } else {
            throw new BusinessException("999999", "按ids删除失败！");
        }
    }

    /**
     * 不分页查询
     * @return
     */
    @Override
    public List<JSONObject> query(String condition, String orderBy){
        condition = DslServiceUtil.transformOperator(condition);
        QueryWrapper<SimpleMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        List<SimpleMap> list = simpleMapService.list(queryWrapper);
        return JsonUtil.transformToJson(list);
    }

    /**
     * 不分页查询
     * @param condition  接收参数
     * @return
     */
    @Override
    public List<JSONObject> queryInfo(String condition, String orderBy) {
        condition = DslServiceUtil.transformOperator(condition);
        QueryWrapper<SimpleMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        queryWrapper.select(SimpleMap.fetchSimpleColumns());
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        List<SimpleMap> list = simpleMapService.list(queryWrapper);
        return JsonUtil.transformToJson(list);
    }

    /**
     * 根据id数组进行简单查询
     *
     * @param ids 参数
     * @return 返回值
     */
    @Override
    public List<JSONObject> queryByIds(List<Long> ids) throws BusinessException {
        List<SimpleMap> SimpleMaps = simpleMapService.listByIds(ids);
        return JsonUtil.transformToJson(SimpleMaps);
    }

    /**
     * 分页查询，去除复杂字段
     * @return 返回统一结果类型(ResultInfo)
     */
    @Override
    public PageInfo queryByPage(int pageNumber, int pageSize, String condition, String orderBy) {
        Page<SimpleMap> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<SimpleMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        return getPageInfoResult(pageInfo, queryWrapper);
    }


    /**
     * 分页查询，去除复杂字段
     * @return
     */
    @Override
    public PageInfo queryInfoByPage(int pageNumber, int pageSize, String condition, String orderBy) {
        Page<SimpleMap> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<SimpleMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(StringUtils.isNotBlank(condition), condition);
        queryWrapper.select(SimpleMap.fetchSimpleColumns());
        DslServiceUtil.buildOrderBy(queryWrapper, orderBy, "updateTime");
        return getPageInfoResult(pageInfo, queryWrapper);
    }

    public PageInfo getPageInfoResult(Page<SimpleMap> pageInfo, QueryWrapper<SimpleMap> queryWrapper) {
        Page<SimpleMap> queryPage = simpleMapService.page(pageInfo, queryWrapper);
        List<SimpleMap> records = queryPage.getRecords();
        List<JSONObject> jsonObjectList = getSimpleMapJsonObjectList(records);
        Page<JSONObject> queryPageDTO = new Page<>();
        BeanUtils.copyProperties(queryPage, queryPageDTO);
        queryPageDTO.setRecords(jsonObjectList);
        PageInfo pageResult = PageInfo.of(queryPageDTO);
        return pageResult;
    }

    /**
     * 将模型列表转换为jsonObject 列表
     *
     * @param SimpleMaps 模型列表
     * @return 返回结果
     */
    public List<JSONObject> getSimpleMapJsonObjectList(List<SimpleMap> SimpleMaps) {
        List<JSONObject> jsonList = new ArrayList<>();
        SimpleMaps.forEach(m -> {
            jsonList.add(m.fetchJsonObject());
        });
        return jsonList;
    }


    /**
     * 从actionData 取出jsonObject列表 转换成模型列表
     *
     * @param jsonObjectList 参数
     * @return 返回模型实体集合
     */
    private List<SimpleMap> transformActionDataList(List<JSONObject> jsonObjectList) {
        List<SimpleMap> simpleMaps = new ArrayList<>();
        jsonObjectList.forEach(j -> {
            SimpleMap simpleMap = new SimpleMap(j);
            simpleMaps.add(simpleMap);
        });
        return simpleMaps;
    }
}
